package com.jy.horny.base

import com.framework.network.manager.NetworkManager
import com.lib.module_base.BaseViewModel
import com.jy.horny.api.ApiService
import com.jy.horny.api.HeadDynamicsParamsHelper


/**
 * @ClassName: BaseVMViewModel
 * @Description:
 * @Author: You
 */
open class BaseVMViewModel : BaseViewModel() {
    val apiService = NetworkManager.getInstance().service as? ApiService

    /**
     * 显示loading
     */
    fun showLoading(message: String? = null) {
        baseCallBack.postValue(Pair(SHOW_PROGRESS, message))
    }

    /**
     * 隐藏loading
     */
    fun hideLoading() {
        baseCallBack.postValue(Pair(HIDE_PROGRESS, null))
    }

    /**
     * @param dynamicsHeadParamKey cannot be null when adding dynamic request header parameters
     */
    fun <T> request(
        httpRequestListener: HttpRequestListener<T>,
        dynamicsHeadParamKey: String? = null,
        request: Request
    ) {
        val map = HashMap<String, Any>()
        val dynamicsHeadParamMap = HashMap<String, Any>()
        httpRequestListener.addRequestParams(map)
        httpRequestListener.addDynamicsHeadParams(dynamicsHeadParamMap)
        //添加动态的请求头参数
        dynamicsHeadParamKey?.let {
            HeadDynamicsParamsHelper.headDynamicsParamMap.put(it, dynamicsHeadParamMap)
        }
        request.request(map) {
            if (dynamicsHeadParamKey != null && HeadDynamicsParamsHelper.headDynamicsParamMap.contains(
                    dynamicsHeadParamKey
                )
            ) {
                HeadDynamicsParamsHelper.headDynamicsParamMap.remove(dynamicsHeadParamKey)
            }
        }
    }

    fun <T> request(
        httpRequestListener: HttpRequestListener<T>,
        dynamicsHeadParamKey: String? = null,
        request: (map: HashMap<String, Any>) -> Unit
    ) {
        val map = HashMap<String, Any>()
        val dynamicsHeadParamMap: HashMap<String, Any>? = null
        httpRequestListener.addRequestParams(map)
        //添加动态的请求头参数
        dynamicsHeadParamMap?.let {
            dynamicsHeadParamKey?.let { it1 ->
                HeadDynamicsParamsHelper.headDynamicsParamMap.put(
                    it1,
                    it
                )
            }
        }
        request.invoke(map)
    }

}